package edu.washington.cs.workflow.spec4j.example;

import java.util.Collection;

import org.uispec4j.UISpec4J;
import org.uispec4j.UISpecTestCase;
import org.uispec4j.Window;
import org.uispec4j.interception.MainClassAdapter;

import edu.washington.cs.workflow.analysis.guistructure.FeedbackDirectedTester;
import edu.washington.cs.workflow.analysis.guistructure.Spec4JUtils;
import edu.washington.cs.workflow.analysis.guistructure.uievents.MenuItemEvent;
import edu.washington.cs.workflow.analysis.guistructure.uievents.UIEvent;
import edu.washington.cs.workflow.util.Utils;

public class TestFreemindProgram extends UISpecTestCase {

	static {
        UISpec4J.init();
    } 
	
	protected void setUp() throws Exception {
		Class<?> clz = Class.forName("freemind.main.FreeMind");
	    setAdapter(new MainClassAdapter(clz, new String[0]));
	}
	
	public void testShowAllActions() {
		System.out.println("getting window");
		Window window = getMainWindow();
		System.err.println(window.getDescription());
		 Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		 System.out.println(events.size()); 
			for(UIEvent event : events) {
				System.err.println(event);
			}
	}
	
	
	public void testTriggerAllActions() {
		UISpec4J.setWindowInterceptionTimeLimit(2000);
		System.out.println("getting window");
		Window window = getMainWindow();
		System.err.println(window.getDescription());
		Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		System.out.println(events.size()); 
		for(UIEvent event : events) {
			if(event.toString().indexOf("File, New") != -1) {
				if(event.canFireEvent()) {
					event.probeWindowInfo();
				}
			}
		}
		
		for(UIEvent event : events) {
			if(event.toString().indexOf("Quit") != -1
					|| event.toString().indexOf("File, New") != -1) {
				continue;
			} else {
				if(event.canFireEvent()) {
					event.probeWindowInfo();
				}
			}
		}
	}
	
	//Export, As HTML
	public void testExportAsHTML() {
		Window window = getMainWindow();
		Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		for(UIEvent event : events) {
			System.err.println(event);
			if(event.toString().indexOf("File, New") != -1) {
				if(event.canFireEvent()) {
					event.probeWindowInfo();
				}
			}
			if(event.toString().indexOf("Export, As HTML") != -1) {
				if(event.canFireEvent()) {
					event.probeWindowInfo();
				} else {
					System.err.println("=================");
				}
			}
		}
	}
	
	//Navigate, Toggle Folded
	public void testToggleFolded() {
		Window window = getMainWindow();
		Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		for(UIEvent event : events) {
			System.err.println(event);
			if(event.toString().indexOf("File, New") != -1) {
				if(event.canFireEvent()) {
					event.probeWindowInfo();
				}
			}
			if(event.toString().indexOf("Navigate, Toggle Folded") != -1) {
				if(event.canFireEvent()) {
					event.probeWindowInfo();
				} else {
					System.err.println("=================");
				}
			}
		}
	}
	
	//Format, Edge Styles, Linear
	public void testLineStyle() {
		Window window = getMainWindow();
		Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		for(UIEvent event : events) {
			
			if(event.toString().indexOf("File, New") != -1) {
				if(event.canFireEvent()) {
					System.err.println(event);
					event.probeWindowInfo();
				}
			}
			if(event.toString().indexOf("Format, Edge Styles, Bezier") != -1) {
				if(event.canFireEvent()) {
					System.err.println(event);
					event.probeWindowInfo();
				} else {
					System.err.println("=================");
				}
			}
		}
	}
	
	//File, Export, Branch as HTML
	public void testBranchesHTML() {
		Window window = getMainWindow();
		Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		for(UIEvent event : events) {
			
			if(event.toString().indexOf("File, New") != -1) {
				if(event.canFireEvent()) {
					System.err.println(event);
					event.probeWindowInfo();
				}
			}
			if(event.toString().indexOf("File, Export, Branch as HTML") != -1) {
				if(event.canFireEvent()) {
					System.err.println(event);
					event.probeWindowInfo();
				} else {
					System.err.println("=================");
				}
			}
		}
	}
	
	public void testEditPreference() {
		Window window = getMainWindow();
		Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		for(UIEvent event : events) {
			System.err.println(event);
			if(event.toString().indexOf("File, New") != -1) {
				if(event.canFireEvent()) {
					event.probeWindowInfo();
				}
			}
			if(event.toString().indexOf("Tools, Preferences") != -1) {
				if(event.canFireEvent()) {
					event.probeWindowInfo();
				} else {
					System.err.println("=================");
				}
			}
		}
	}
	
	//Maps, next map
	public void testNextPreviousMaps() {
		Window window = getMainWindow();
		Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		for(UIEvent event : events) {
			System.err.println(event);
			if(event.toString().indexOf("File, New") != -1) {
				if(event.canFireEvent()) {
					event.probeWindowInfo();
					event.fireEvent();
				}
			}
			if(event.toString().indexOf("Maps, Next Map") != -1) {
				if(event.canFireEvent()) {
					event.probeWindowInfo();
				} else {
					System.err.println("=================");
				}
			}
		}
	}
	
	//Zoom In,
	public void testZoomIn() {
		Window window = getMainWindow();
		Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		for(UIEvent event : events) {
			System.err.println(event);
			if(event.toString().indexOf("File, New") != -1) {
				if(event.canFireEvent()) {
					event.probeWindowInfo();
					event.fireEvent();
				}
			}
			if(event.toString().indexOf("View, Zoom In") != -1) {
				if(event.canFireEvent()) {
					event.probeWindowInfo();
				} else {
					System.err.println("=================");
				}
			}
		}
	}
	
//	public static void main(String[] args) {
//		freemind.main.FreeMind.main(new String[]{});
//	}
	
}